﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using Microsoft.Phone.Tasks;
using AMobiSDK;

namespace Sai_Se_Dung
{
    public partial class Lose : PhoneApplicationPage
    {
        public Lose()
        {
            InitializeComponent();
            
            txtLoseScore.Text = Play.m_curScore.ToString();
            
            // Save best score (if any)
            if (Play.m_curScore > new Score().ReadScore())
            {
                new Score().WriteScore(Play.m_curScore.ToString());
            }

            //if (Play.m_curLevel == 2)
            //{
            //    FeedbackOverlay.VisibilityChanged += FeedbackOverlay_VisibilityChanged;

            //    if (FeedbackOverlay.Equals(ResourceRateApp.RatingYes))
            //    {
            //        // Cho thêm 1 cơ hội chơi tiếp ở màn 2
            //        new Play().SetCurrentLevel(); 

            //        if (NavigationService.CanGoBack)
            //        {
            //            while (NavigationService.RemoveBackEntry() != null)
            //            {
            //                NavigationService.RemoveBackEntry();
            //            }
            //        }
            //        NavigationService.Navigate(new Uri("/Play.xaml", UriKind.Relative));
            //    }
            //}
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (Option.isOn == true)
            {
                mp3_lose.AutoPlay = true;
                mp3_lose.Play();
            }
            else
            {
                mp3_lose.AutoPlay = false;
                mp3_lose.Stop();
            }

            AdManager.setWidgetCode("f2257ad5570a4a773a2edd33504ef3c2");//Bạn phải thay mã này bằng widgetCode của bạn
            AdManager advertisement = new AdManager();
            advertisement.OnNavigationTo(this, null, null, banner);
        }
           
        /// <summary>
        /// Re-Play again
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPlayAgain_Click(object sender, RoutedEventArgs e)
        {           
            if (Option.isOn == true)
            {
                mp3_btn.Play();
            }

            new Play().Reset();

            if (NavigationService.CanGoBack)
            {
                while (NavigationService.RemoveBackEntry() != null)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            NavigationService.Navigate(new Uri("/Play.xaml", UriKind.Relative));
        }

        /// <summary>
        /// Override nút BACK
        /// </summary>
        /// <param name="e"></param>
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (Option.isOn == true)
            {
                mp3_btn.Play();
            }

            new Play().Reset();

            if (NavigationService.CanGoBack)
            {
                while (NavigationService.RemoveBackEntry() != null)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            NavigationService.Navigate(new Uri("/Confirm.xaml", UriKind.Relative));
        }

        /// <summary>
        /// Share Score to Facebook
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShareScore_Click(object sender, RoutedEventArgs e)
        {
            ShareLinkTask shareLinkTask = new ShareLinkTask();
            shareLinkTask.Title = "SAI sẽ ĐÚNG";
            shareLinkTask.LinkUri = new Uri("http://www.windowsphone.com/s?appid=8229aaca-73ee-4d2a-8e51-a36508effa21", UriKind.Absolute);//cho linh cua minh vao
            shareLinkTask.Message = "Mình vừa đạt được " + Play.m_curScore.ToString() + " điểm!";
            shareLinkTask.Show();
        }

        /// <summary>
        /// Rate apps
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FeedbackOverlay_VisibilityChanged(object sender, EventArgs e)
        {
            //ApplicationBar.IsVisible = (FeedbackOverlay.Visibility != Visibility.Visible);
        }
    }
}